package com.wxg.address.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
 * Helper functions for handling dates.
 *
 * @author Wxg
 *
 */
public class DateUtil {
	/**
	 * The date pattern that is used for conversion. Change as you wish.
	 */
	private static final String DATE_PATTERN = "dd.MM.yyyy";

	/**
	 * The date formatter.
	 */
	private static final DateTimeFormatter DATE_FORMATTER =
			DateTimeFormatter.ofPattern(DATE_PATTERN);

	/**
	 * Returns the given date as a well formatted String.
	 * The above defined {@link DateUtil#DATE_PATTERN} is used.
	 *
	 * @param date : the date to be returned as a string
	 * @return formatted string
	 */
	public static String format(LocalDate date){
		if(date == null){
			return null;
		}
		return DATE_FORMATTER.format(date);
	}

	/**
	 * Coverts a String in the format of the defined {@link DateUtil#DATE_PATTERN}
	 * to a {@linkplain LocalDate} object.
	 *
	 * Returns null if the String could not be converted.
	 *
	 * @param dateString : the date as String
	 * @return the date object or null if it could not be converted
	 */
	public static LocalDate parse(String dateString){
		try {
			return DATE_FORMATTER.parse(dateString, LocalDate::from);
		} catch (DateTimeParseException e) {
			return null;
		}
	}

	/**
	 * Checks the String whether it is a valid date.
	 *
	 * @param dateString
	 * @return true is the String is a valid date
	 */
	public static boolean validDate(String dateString){
		// Try to parse the String.
		return DateUtil.parse(dateString) != null;
	}

}
